package cn.kongwg.controller;

import cn.kongwg.entity.Books;
import cn.kongwg.service.BookService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kongwg
 * @create 2021-03-11 15:51
 */
@Controller
@RequestMapping("/book")
@Slf4j
public class BookController {


    @Autowired
    private BookService bookService;

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        log.trace("this is trace");
        log.info("this is info");
        log.debug("this is debug");
        log.warn("this is warn");
        log.error("this is error");
        bookService.findAllBook();
        return "hello";
    }


    @GetMapping("/toAdd")
    public String toAdd() {
        return "addBook";
    }

    @PostMapping("/add")
    public String addBook(Books book) {
        bookService.addBook(book);
        return "redirect:/book/get/all";
    }


    @GetMapping("/delete/{id}")
    public String deleteBook(@PathVariable("id") Integer id) {
        bookService.deleteBook(id);
        return "redirect:/book/get/all";

    }

    @GetMapping("/toUpdate")
    public String toUpdate(Model model, @RequestParam("id") int id) {

        Books book = bookService.findBookById(id);

        model.addAttribute("book", book);
        return "updateBook";
    }

    @PostMapping("/update")
    public String updateBook(Books book) {
        bookService.updateBook(book);
        return "redirect:/book/get/all";

    }

    @GetMapping("/get/{id}")
    public String getBook(@PathVariable("id") int id) {
        System.out.println(id);
        Books book = bookService.findBookById(id);
        String str = JSON.toJSONString(book);

        return str;
    }

    @GetMapping("/get/all")
    public String getAllBook(Model model) {
        List<Books> allBook = bookService.findAllBook();
        model.addAttribute("list", allBook);
        return "allBook";
    }


    @GetMapping("/query")
    public String queryBook(@RequestParam("bookName") String bookName, Model model) {
        List<Books> books = bookService.queryBookByName(bookName);

        System.out.println(books);
        System.out.println(books.size());
        if (books.size() == 0) {
            List<Books> allbook = bookService.findAllBook();
            model.addAttribute("list", allbook);
            model.addAttribute("error", "未查询到");
        } else {
            model.addAttribute("list", books);
        }
        return "allBook";
    }

    @GetMapping("/page")
    @ResponseBody
    public String pageBook(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageInfo<Books> booksPageInfo = bookService.pageBook(pageNum, pageSize);
        String pageBook = JSON.toJSONString(booksPageInfo);
        return pageBook;
    }
}
